<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ page import="com.DBCon" %>
    <%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<%@ include file="../bootstrap.jsp"%>
<style>
.navbar{
padding-bottom:0;
}
</style>
<title>学院管理</title>
</head>
<body>
	<%
	//查询数据
	String sql="select DepName,DepNum,DepCOnName,DepConEmail from department Order By DepNum";
	try{
	ResultSet rs = new DBCon().find(sql);

	//获取页码
	int item = 7; //每一页显示数据项
	int pg = 1; //初始页码
	String p1 =(String)request.getAttribute("p");
	String p2 =request.getParameter("p");
	//如果接收的是页面传递的参数(Parameter 返回的一定是字符串)
	if (p1 != null && p1.length() > 0) {
		pg = Integer.parseInt(p1);
	}else if(p2 != null && p2.length() > 0){
		//如果接收的是Servlet传递的参数(Attribute 返回的是一个对象)
		pg = Integer.parseInt(p2);
	}
	//计算起始位置和结束位置
	rs.last();
	int count = rs.getRow(); //获取全部记录行数
	System.out.println(count);

	int start = (pg - 1) * item;
	int limit = pg * item < count ? pg * item : count;
	if (start > 0) {
		rs.absolute(start);
	} else {
		rs.beforeFirst();
	}
	System.out.println(start + "-" + limit);
	%>
	<h4 style="font-weight: bolder; left: 20px">学院列表</h4>
		<nav class="navbar navbar-light bg-light">
			<ul class="pagination">
				<!-- 显示上一页和下一页分页按钮 -->
				<%
				if (pg == 1) {
				%>
				<li class="page-item disabled "><a class="page-link">上一页</a></li>
				<%
				}
				if (pg > 1) {
				%>
				<li class="page-item "><a class="page-link"
					href="DepEdit.jsp?p=<%=pg - 1%>">上一页</a></li>
				<%
				}
				if (pg * item < count) {
				%>
				<li class="page-item"><a class="page-link"
					href="DepEdit.jsp?p=<%=pg + 1%>">下一页</a></li>
				<%
				} else if (pg * item >= count) {
				%>
				<li class="page-item disabled"><a class="page-link">下一页</a></li>
				<%
				}
				%>
			</ul>
			<a align="right" href="AddDep.jsp?pg=<%=pg%>">在此新增学院账号</a>
		</nav>
	<table class="table table-striped">
		<tr align="center">
			<th scope="col">学院名称</th>
			<th scope="col">学院账号</th>
			<th scope="col">学院联系人</th>
			<th scope="col">学院联系人邮箱</th>
			<th>操作</th>
		</tr>
		<%
		while (rs.next() && rs.getRow() <= limit) {
		%>
		<tr align="center">
			<td><%=rs.getString("DepName")%></td>
			<td><%=rs.getString("DepNum")%></td>
			<td><%=rs.getString("DepConName")%></td>
			<td><%=rs.getString("DepConEmail")%></td>
			<td><a  href="DepInfoEdit.jsp?usernum=<%=rs.getString("DepNum")%>&userdepname=<%=rs.getString("DepName")%>&pg=<%=pg%>">编辑</a>
			    <a  href="Intitalize?usernum=<%=rs.getString("DepNum")%>&pg=<%=pg%>"
			       onClick="return confirm('是否初始化<%=rs.getString("DepName")%>账号密码')">初始化</a>
				<a href="DelDep?usernum=<%=rs.getString("DepNum")%>&pg=<%=pg%>"
				onClick="return confirm('是否删除<%=rs.getString("DepName")%>账号')">删除</a></td>
		</tr>
		<%}
		}catch(Exception e){
			// TODO Auto-generated catch block
			e.printStackTrace();
		}%>
	</table>
</body>
</html>